$(function () {
    $(document).keyup(function (e) {
        if (e.which == 13) {
            $('#selectBtnEnter').click();
        }
    })
});
var path = '../';
var pageListUrl = path + 'businessPrice/list';
var pageUrl = path + 'businessPrice/';

//数据校验逻辑
var advPrice = {
    checkData: function (dataObj) {
        //清除历史错误提示
        dataObj.priceErr = "";
        dataObj.advChannelErr = "";
        var isSubmit = true;

        if (dataObj.advChannel == '') {
            dataObj.advChannelErr = "请选择广告商";
            isSubmit = false;
        }

        var reg = /^\d+(\.\d{1,2})?$/;
        if (!reg.test(dataObj.price)) {
            dataObj.priceErr = "价格只能为正整数或小数（保留1位）";
            isSubmit = false;
        }

        return isSubmit;
    }
};

//绑定列表List---begin----所有的操作都围绕列表展开，新增、更新其他比较独立
var dataList = new Vue({
    el: "#advPriceList",
    data: {
        pageInfo: {},
        dictBusiness: [],
        //TODO 自定义条件查询---begin--------
        condition: {
            page: 1,
            rows: customUtils.pageSize,
            items: [],
            all: false,
            startDate: "",
            overDate: "",
            status: "",
            equipmentSource: "",
            equipmentCode: ""
        },
        //自定义条件查询---end--------
        selectIds: [],//多选
        showSuccess: false,//false不显示成功提示
        selectedIdx: -1,//保存操作行的索引
        isSearch: false,//init:页面初始化；search:点击查询；paging:点击翻页；changePageSize:改变页面显示条数
        selectedPageSize: customUtils.pageSize//修改页面数据显示条数
    },
    mounted: function () {
        //初始化页面数据
        this.getList('init');
        this.getBusinessType();
    },
    methods: {
        changeTime: function (elem) {
            dataList.$data.condition[elem.split('-')[0]] = $("#" + elem).val();
            console.log(dataList.$data.condition);
        },
        getBusinessType: function () {
            this.$http.get("../equipment/queryBusinessType").then(function (response) {
                console.log(response)
                dataList.$data.dictBusiness = response.body;
                console.log(addOrUpdate.$data.dictBusiness)
            }, function (res) {

            });
        },
        //搜索、分页列表
        getList: function (eventSrc) {
            var self = this;
            //改变页面显示条数
            if (eventSrc == "changePageSize") {
                if (this.pageInfo.pageSize != this.selectedPageSize) {
                    this.pageInfo.pageSize = this.selectedPageSize;
                    $('#pagination').jqPaginator('destroy');
                    eventSrc = 'init';//重新绘制控件
                } else {
                    return;
                }
            }
            //初始化时无当前页数，后台有默认第1页，每页10条
            if (this.pageInfo != undefined && this.pageInfo.pageNum != undefined) {
                self.condition["page"] = this.pageInfo.pageNum;
                self.condition["rows"] = this.pageInfo.pageSize;
            }
            var param = {};
            param["page"] = self.condition["page"];
            param["rows"] = self.condition["rows"];
            param["startDate"] = self.condition["startDate"];
            param["overDate"] = self.condition["overDate"];
            param["status"] = self.condition["status"];
            param["equipmentSource"] = self.condition["equipmentSource"];
            param["equipmentCode"] = self.condition["equipmentCode"];
            this.condition.items = [];
            console.log(param);
            //查询请求
            //发送请求,第一个参数http对象，第二个是url，第三个是参数，第四个是成功回调，第五个是请求失败回调
            self.$http.post(pageListUrl, param).then(function (response) {
                console.log(param);
                console.log(pageListUrl);
                dataList.pageInfo = response.body.pagination;
                customUtils.noData('无数据', dataList.pageInfo.pages);
                //页面初始化、搜索时，才需要执行分页控件
                if (eventSrc == 'init' || eventSrc == 'search') {
                    customUtils.paging('#pagination', dataList.pageInfo.pages, customUtils.showPage, function (num) {
                        //当前页码pageNum与将要翻页的页码num不相等才执行，否则为同一页
                        if (dataList.pageInfo.pageNum != num) {
                            dataList.pageInfo.pageNum = num;
                            dataList.getList('paging');
                        }
                    });
                }
            });
        },
        //打开添加对话框
        addOrUpdatePanel: function (flag) {
            console.log(flag);
            //清除表单数据，否则会有前一条修改数据的内容
            $.each(addOrUpdate.$data, function (key) {
                addOrUpdate[key] = "";
            });
            if (flag != 'update') {
                addOrUpdate.$data.id = "";
                var tmpTitle = "添加";
                $("#t1").show();
            }
            if (flag == 'update') {
                var tmpTitle = "修改";
                $("#t1").hide();
            }
            // var tmpTitle = flag == "add" ? "添加" : "修改";
            addPanel = layer.open({
                type: 1,
                title: tmpTitle + "服务商购入价",
                area: ['570px', '70%'],
                shadeClose: false, // 点击遮罩关闭
                content: $('#addPanel')
            });
        },
        //初始化更新，更新对话框
        updateInit: function (item) {
            //先显示窗口，才有加载效果
            this.addOrUpdatePanel("update");
            //回显表单数据
            $.each(item, function (key) {
                if (addOrUpdate[key] !== undefined && item[key]) {
                    addOrUpdate[key] = item[key];
                }
            });
        }
    }
});
//绑定列表List---end----

//新增或更新---------begin-------
var addOrUpdate = new Vue({
    el: "#addPanel",
    data: {
        id: "",
        price: "",
        advChannel: "",

        priceErr: "",
        advChannelErr: "",
        showSuccess: true,//显示成功提示
    },
    methods: {
        changeTime: function (elem) {
            addOrUpdate[elem.split('-')[0]] = $("#" + elem).val();
        },
        save: function () {
            //逻辑校验
            if (!advPrice.checkData(this.$data)) {
                return;
            }
            //没有id的时候表示新增，否则为更新
            var actionUrl = this.id == "" ? 'save' : 'update';
            var self = this;
            customUtils.post(self, pageUrl + actionUrl, self.$data, function (response, isSuccess) {
                customUtils.showTips(response.body, addOrUpdate.$data, 3000)
                console.log(response)
                console.log(isSuccess);
                dataList.getList('no');
                if (isSuccess && addPanel) layer.close(addPanel);
            }, function (res) {
                //console.log(response.body);
            });
        },
        //清除表单数据
        reset: function () {
            $.each(addOrUpdate.$data, function (key) {
                addOrUpdate[key] = "";
            });
        }
    }
});
//新增或更新---------end-------